package ac.leedsmet.devmanager.core;

import org.apache.log4j.Logger;
import java.util.LinkedList;
import java.util.List;

/**
 *  This class is creating a new project issue after it makes sure that there is a project attached on the specific issue.
 *  After the issue creation the auto increment method is giving a unique issue ID on the created issue.
 * @author Samael
 * @version 1.0
 */
public class IssueManagerImpl implements IssueManager {

    protected static final Logger log = Logger.getLogger(IssueManagerImpl.class);
    private static long issueId = 100000L;
    List<Issue> issues = new LinkedList<Issue>();
    Project project;

    public IssueManagerImpl(Project project) {
        this.project = project;
    }

    /**
     * Is creating an issue after it makes sure that there is a project for that issue and is giving
     * a unique id on that issue as well
     * @param title the tittle of the new created issue
     * @param creator the creator's name
     * @return the new created issue
     */
    @Override
    public Issue createIssue(String title, String creator) {
        issueId++;
        Issue issue = new Issue(title, creator, issueId);
        this.issues.add(issue);
        if(project.addIssue(issue)) {
            log.debug("Successfully added \"" + title + "\" issue to \"" + project.getName() + "\" project");
        }
        return issue;
    }

    /**
     * @param issueId The issue id of the Issue object that is required
     * @return Returns the issue for the given issue Id.
     */
    public Issue getIssue(final Long issueId) {
        if(issueId == null) {
            throw new NullPointerException("null issue Id");
        }
        for(Issue issue : issues) {
            if (issue.getIssueId() == issueId)
                return issue;
        }
        return null;
    }


}
